package com.ulic.customerManApplication.membermodule.dao.impl;

import java.util.Collection;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Component;

import com.ulic.customerManApplication.base.BaseDao;
import com.ulic.customerManApplication.constant.CONSTANT;
import com.ulic.customerManApplication.membermodule.bo.Member;
import com.ulic.customerManApplication.membermodule.bo.SubMember;
import com.ulic.customerManApplication.membermodule.dao.MemberExtDao;
import com.ulic.customerManApplication.membermodule.vo.SubMemberVO;
import com.ulic.customerManApplication.pager.PagerModel;

@Component("memberExtDao")
public class MemberExtDaoImpl extends BaseDao implements MemberExtDao {

	@Override
	public List<Member> getMembersByPhone(Collection<String> phones) {
		Session session = getHibernateTemplate().getSessionFactory().getCurrentSession();
		Criteria cri = session.createCriteria(Member.class);
		List<Member> list = cri.add(Restrictions.in("cellPhone", phones)).list();
		return list;
	}

	@Override
	public List<Member> getMembersByIdCard(Collection<String> idCards) {
		Session session = getHibernateTemplate().getSessionFactory().getCurrentSession();
		Criteria cri = session.createCriteria(Member.class);
		List<Member> list = cri.add(Restrictions.in("idCard", idCards)).list();
		return list;
	}

	@Override
	public List<Member> getMembersByName(Collection<String> names) {
		Session session = getHibernateTemplate().getSessionFactory().getCurrentSession();
		Criteria cri = session.createCriteria(Member.class);
		List<Member> list = cri.add(Restrictions.in("memberName", names)).list();
		return list;
	}

	@Override
	public List<Member> getMembersById(Collection<Integer> ids) {
		Session session = getHibernateTemplate().getSessionFactory().getCurrentSession();
		Criteria cri = session.createCriteria(Member.class);
		List<Member> list = cri.add(Restrictions.in("id", ids)).list();
		return list;
	}

	@Override
	public void saveOrUpdateMembers(Collection<Member> members) {
		getHibernateTemplate().saveOrUpdateAll(members);
	}

	@Override
	public void saveSubMembers(Collection<SubMember> subMembers) {
		getHibernateTemplate().saveOrUpdateAll(subMembers);
	}

	@Override
	public List<SubMember> getSubMembersByMember(Integer id) {
		Session session = getHibernateTemplate().getSessionFactory().getCurrentSession();
		Criteria cri = session.createCriteria(SubMember.class);
		List<SubMember> list = cri.add(Restrictions.eq("member.id", id)).list();
		return list;
	}

	@Override
	public PagerModel getMemberEnjoySubMembers(SubMemberVO subMemberVO,
			int curPage) {
		StringBuilder sb = new StringBuilder();
		sb.append(" from SubMember subMember where subMember.sub_memberEnjoyActivity.id="+subMemberVO.getMemberEnjoyId());
		SubMember subMember = subMemberVO.getSubMember();
		if(subMember != null){
			if(! "No".equals(subMember.getSub_gender())){
				sb.append(" and subMember.sub_gender = '"+subMember.getSub_gender()+"'");
			}
			if(subMember.getSubCompany()!= null ){
				if(! "No".equals(subMember.getSubCompany())){
					sb.append(" and subMember.subCompany = '"+subMember.getSubCompany()+"'");
				}
			}
			if(subMember.getSubCity()!= null ){
				if(! "No".equals(subMember.getSubCity())){
					sb.append(" and subMember.subCity = '"+subMember.getSubCity()+"'");
				}
			}
			if(! "No".equals(subMember.getSub_memberType())){
				sb.append(" and subMember.sub_memberType = '"+subMember.getSub_memberType()+"'");
			}
			if(subMember.getSub_memberName()!=null && subMember.getSub_memberName().length()!=0){
				sb.append(" and subMember.sub_memberName like '%"+subMember.getSub_memberName()+"%'");
			}
			if(subMember.getSub_carType()!=null && subMember.getSub_carType().length()!=0){
				sb.append(" and subMember.sub_carBrand like '%"+subMember.getSub_carType()+"%'");
			}
			if(subMemberVO.getStartbirthday()!=null &&subMemberVO.getStartbirthday().length()!=0){
				sb.append(" and to_date(subMember.sub_birthday,'YYYY-MM-DD  HH24:MI:SS') >= to_date('"+subMemberVO.getStartbirthday()+" 00:00:00"+"','YYYY-MM-DD  HH24:MI:SS') ");
			}
			if(subMemberVO.getEndbirthday()!=null &&subMemberVO.getEndbirthday().length()!=0){
				sb.append(" and to_date(subMember.sub_birthday,'YYYY-MM-DD  HH24:MI:SS') <= to_date('"+subMemberVO.getEndbirthday()+" 23:59:59"+"','YYYY-MM-DD  HH24:MI:SS') ");
			}
			
			if(subMemberVO.getStartcreate()!=null &&subMemberVO.getStartcreate().length()!=0){
				sb.append(" and subMember.sub_createDate>= to_date('"+subMemberVO.getStartcreate()+" 00:00:00"+"','YYYY-MM-DD  HH24:MI:SS') ");
			}
			if(subMemberVO.getEndcreate()!=null &&subMemberVO.getEndcreate().length()!=0){
				sb.append(" and subMember.sub_createDate <= to_date('"+subMemberVO.getEndcreate()+" 23:59:59"+"','YYYY-MM-DD  HH24:MI:SS') ");
			}
			 
		}
		sb.append(" order by subMember.sub_donateStuNum desc");
		return searchPaginated(sb.toString(), curPage, CONSTANT.PageSize);
	
	}

	@Override
	public SubMember getMemberEnjoySubMemberByPhone(String sub_cellPhone,
			String memberEnjoyId) {
		Session session = getHibernateTemplate().getSessionFactory().getCurrentSession();
		Criteria cri = session.createCriteria(SubMember.class);
		List<SubMember> list = cri.add(Restrictions.eq("sub_memberEnjoyActivity.id", Integer.parseInt(memberEnjoyId))).add(Restrictions.eq("sub_cellPhone", sub_cellPhone)).list();
		if(list != null && list.size() > 0){
			return list.get(0);
		}
		return null;
	}

	@Override
	public List<SubMember> getAllSubMember(){
		String hql = "select t from SubMember t";
		return hibernateTemplate.find(hql);
	}

	@Override
	public List<Member> getAllMember() {
		String hql = "select t from Member t";
		return hibernateTemplate.find(hql);
	}

	@Override
	public void saveOrUpdateSubMember(Collection<SubMember> subMembers) {
		getHibernateTemplate().saveOrUpdateAll(subMembers);
	}

	@Override
	public List<SubMember> getNoMemberSubMember() {
		String hql = "select t from SubMember t where t.member is null";
		return hibernateTemplate.find(hql);
	}
	
}
